Tags: #◇Computer Science #◇CCPROG2 #◇C Programming #◇Pointers #◇Reviewer #◇Finals
CCPROG2 Comprehensive Finals Reviewer
◇Overview
This reviewer consolidates Advanced C Programming (CCPROG2). Core topics include pointer arithmetic and dereferencing, multi-dimensional arrays, strings and string library functions, user-defined structures and unions, dynamic memory allocation (malloc, calloc, realloc, free), file handling (fopen, fread, fwrite, fprintf, fscanf), and recursion.
◇1. Pointers & Memory Architecture
Pointer Operators & Equivalences
- Address-of
&x: Returns the physical memory address of variablex. - Dereference
*p: Accesses the value stored at memory address stored inp. - Array-Pointer Identity: For array
arr,arr[i] \equiv *(arr + i) \equiv *(i + arr) \equiv i[arr]. - Pointer Arithmetic: If
pisT*,p + 1increments address bysizeof(T)bytes!
◇2. Dynamic Memory Management Functions
| Function Prototype | Description | Return on Failure |
|---|---|---|
void* malloc(size_t size); | Allocates uninitialized heap memory of size bytes. | NULL |
void* calloc(size_t num, size_t size); | Allocates and zero-initializes num * size bytes. | NULL |
void* realloc(void *ptr, size_t new_size); | Resizes an allocated block; preserves existing content. | NULL (original block remains valid) |
void free(void *ptr); | Releases allocated heap memory back to the OS. | void (Passing NULL is a safe no-op) |
[!WARNING] Common Memory Vulnerabilities
- Dangling Pointer: A pointer that points to deallocated memory after
free(p). (Fix:free(p); p = NULL;).- Memory Leak: Allocated memory on heap lost when all pointers to it go out of scope without calling
free().- Double Free: Calling
free()twice on the same pointer causes heap corruption / crashes.
◇3. Structures & File I/O Summary
Structure Access
- Given
struct Student s;Dot operator:s.id. - Given
struct Student *p = &s;Arrow operator:p->id(equivalent to(*p).id).
File Streams
- Opening:
FILE *fp = fopen("data.bin", "rb");(Modes:"r","w","a","rb","wb"). Always checkif (fp == NULL). - Text I/O:
fprintf(fp, ...),fscanf(fp, ...),fgets(buffer, size, fp). - Binary I/O:
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
- Closing:
fclose(fp);flushes buffers and releases file descriptor.
◇4. High-Yield Exam Checklist
- Does
reallocoverwrite the original pointer immediately? Check ifvoid *temp = realloc(...)to prevent leak on failure! - In
char str[] = "Hello";, what issizeof(str)? bytes (includes null terminator\0). - What is
strlen("Hello")? (excludes\0). - In
struct, does member ordering affect memory footprint? YES (Structure padding and alignment). - Did you check
feof(fp)before or after reading? Check after read operation!
◇References & Navigation
- Previous Topic: ◇CCPROG1
- Next Topic: ◇CCDSALG, ◇CCPROG3
- Prerequisites: ◇Advanced C, Pointers, Dynamic Memory, Structs
- Cross-Curricular Connections: ◇CSOPESY
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
Given int arr[5] = {10, 20, 30, 40, 50}; int *ptr = arr + 2;, what is the value of *(ptr - 1)?
Which memory allocation function initializes all allocated bytes to zero?
Given the declaration typedef struct { char name[20]; int age; } Person; Person p, *ptr = &p;, which expression is valid?
What is the return value of fread(buf, sizeof(int), 10, fp) if the file only has 4 integers remaining?
What happens when calling free(p) on a pointer p that is NULL?